package com.sinping.qh.api.admin.record;

import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.dto.admin.noticebook.NoticeBookReturnDto;
import com.sinping.qh.dto.admin.record.GenerateReportByHiddendangerRecordDto;
import com.sinping.qh.service.admin.IHiddenDangerRecordService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 隐患记录
 *
 * @author Tiansx
 * @create 2018-06-05 15:01
 **/
@Api(value = "Admin检查管理", description = "Admin检查管理")
@RestController
@RequestMapping(value = Const.ADMIN_URI, name = "Admin检查管理")
@Slf4j
public class HiddenDangerRecordEndPoint {

    @Autowired
    private Validator globalValidator;

    @Autowired
    private IHiddenDangerRecordService iHiddenDangerRecordService;
    private final static Logger logger = LoggerFactory.getLogger(HiddenDangerRecordEndPoint.class);
    @ApiOperation(value = "获取隐患记录列表", notes = "检查记录状态为：已审核，check_status：3 （字段必传）；hidden_danger_status：隐患状态；")
    @RequestMapping(value = "hidden_danger", method = RequestMethod.GET, name = "获取隐患记录列表")
    public ResDto getHiddenDangerRecordList( @ApiParam(name = "msg", value = "{\"page_size\":10,\"page_current\":1,\"order_column\":null,\"order_by\":null" +
            ", \"condition\":{\"check_unit_id\":\"log\",\"check_building_id\":\"1\",\"check_status\":\"3\"," +
            "<br>\"hidden_danger_status\":[\"xx\"],\"offices\":\"1\",\"start_date\":\"1\",\"end_date\":\"1\"}}", required = true)@RequestParam(required = true) String msg) {

        try {
            logger.info("入参 {}", msg);
            FrontPage page = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);
            if (page == null) {
                log.info("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "msg.param.error");
            }
             return iHiddenDangerRecordService.getHiddenDangerRecordList(page);
        } catch (Exception e) {
            logger.error("数据解析异常{}", e);
            ResDto resDto = new ResDto(Const.SERVER_ERROR, "数据解析异常");
            return resDto;
        }



    }

    /**
     * 获取隐患记录详情
     * @param id
     * @return
     */
    @ApiOperation(value = "获取隐患记录详情", notes = "检查记录状态为：已审核")
    @RequestMapping(value = "hidden_danger/{id}", method = RequestMethod.GET, name = "获取隐患记录详情")
    public ResDto getHiddenDangerRecordDetail(@ApiParam(name = "id", value = "245ad13d6ad4") @PathVariable String id){
        if (StringUtils.isBlank(id)){
            return new ResDto(Const.PARAM_ERROR,"参数id为空");
        }

        return iHiddenDangerRecordService.getHiddenDangerRecordDetail(id);

    }


    /**
     * 根据隐患记录--生成整改报告
     * @param msg
     * @return
     */
    @ApiOperation(value = "生成整改报告word(隐患记录)", notes = "hidden_danger_ids：隐患记录id的集合")
    @RequestMapping(value = "hidden_danger/generate_report", method = RequestMethod.GET, name = "生成整改报告word(隐患记录)")
    public ResDto generateReportByHiddenDangerRecordList( @ApiParam(name = "msg", value = "{\"hidden_danger_ids\":[\"id\",\"id\"]}", required = true)@RequestParam(required = true) String msg) {

        try {
            logger.info("入参 {}", msg);
            GenerateReportByHiddendangerRecordDto reportDto = JsonMapper.defaultMapper().fromJson(msg, GenerateReportByHiddendangerRecordDto.class);
            if (reportDto == null) {
                log.info("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "msg.param.error");
            }
            //校验数据
            Set<ConstraintViolation<GenerateReportByHiddendangerRecordDto>> validateSet = globalValidator.validate(reportDto);
            if (!validateSet.isEmpty()) {
                StringBuffer buffer = new StringBuffer(64);
                for (ConstraintViolation<GenerateReportByHiddendangerRecordDto> apiViolation : validateSet) {
                    buffer.append(apiViolation.getMessage());
                }
                log.info("必传参数 :{}", buffer.toString());
                ResDto resDto = new ResDto();
                resDto.setStatus(Const.PARAM_ERROR);
                resDto.setMsg(buffer.toString());
                return resDto;
            }
            return iHiddenDangerRecordService.getGenerateReport(reportDto);
        } catch (Exception e) {
            logger.error("数据解析异常{}", e);
            ResDto resDto = new ResDto(Const.SERVER_ERROR, "数据解析异常");
            return resDto;
        }

    }



}
